package com.wu.Backstage.controller;

import cn.hutool.core.date.DateUtil;
import com.alibaba.fastjson.JSON;
import com.wu.Backstage.check.MiddleCommoditCheck;
import com.wu.Backstage.check.MiddlelayerCheck;
import com.wu.Backstage.entity.Po.ClickRecord;
import com.wu.Backstage.entity.Po.CommodityEntityPo;
import com.wu.Backstage.entity.Po.evaluated;
import com.wu.Backstage.entity.UserAccount;
import com.wu.Backstage.service.CommodityService;
import com.wu.Backstage.service.OrderDetailsService;
import com.wu.Backstage.service.UserService;
import com.wu.Backstage.tool.PageHelperUtil;
import com.wu.Backstage.tool.Tool;
import com.wu.Backstage.util.JsonUtil;
import com.wu.Backstage.util.Utils;
import net.sf.json.JSONObject;
import org.apache.commons.beanutils.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.lang.reflect.InvocationTargetException;
import java.util.*;

/**
 * @author wujian
 * @version 1.0
 * @date 2021/12/27 0027 23:45
 */

@Controller
@RequestMapping(value = "/person")
public class PerSonController {

    @Autowired
    private MiddleCommoditCheck middleCommoditCheck;

    @Autowired
    private CommodityService commodityService;

    @Autowired
    private MiddlelayerCheck MiddlelayerCheck;

    @Autowired
    private HttpServletResponse response;

    @Autowired
    private MiddlelayerCheck oftenCheckf;

    @Autowired
    private OrderDetailsService orderDetailsService;

    @Autowired
    private UserService userService;


    @Autowired
    private com.wu.Backstage.service.comprehensiveService comprehensiveService;



    private Tool result;



    /**商品信息上传**/
    @RequestMapping(value = "/setperson", method = {RequestMethod.POST, RequestMethod.GET})
    @ResponseBody
    @CrossOrigin
    private void Setperson(HttpServletRequest request) {
        String str = request.getParameter("tab");
        JSONObject jsonObject = JSONObject.fromObject(JSON.parseObject(str));
        Tool result = middleCommoditCheck.setCommodit(jsonObject);
        System.out.println(result);
        Tres(result);
    }



//    /**商品信息查询**/
//    @RequestMapping(value = "/getfindName", method = {RequestMethod.POST, RequestMethod.GET})
//    @ResponseBody
//    @CrossOrigin
//    private void Setperson(Integer sta) {
//
//        List<CommodityEntityPo> commodity = commodityService.findBystat(0);
//        if(commodity!=null || commodity.size()>0)
//        {
//            Tool result = oftenCheckf.isUserAccount(commodity,"查询成功!");
//            Tres(result);
//        }
//
//    }



    @RequestMapping(value = "/findBystatPage", method = {RequestMethod.POST, RequestMethod.GET})
    @ResponseBody
    @CrossOrigin
    public void findBystatPage(Integer sta,Integer st,int page,int pageSize){

        PageHelperUtil<CommodityEntityPo> resultT = commodityService.findBystatPage(sta,st,page,pageSize);
        if(resultT!=null){
            result =oftenCheckf.isUserAccount(resultT,"查询成功!");
        }else{
            result = oftenCheckf.isUserAccount(null,"查询失败!");
        }
        Tres(result);

    }


    /**根据id删除商品**/
    @RequestMapping(value = "/deletePerson", method = {RequestMethod.POST, RequestMethod.GET})
    @ResponseBody
    @CrossOrigin
    public void deletePerson(String id)
    {
        Tool result;
        Integer isId = Integer.parseInt(id);
        if(commodityService.findById(isId)!=null)
        {
            commodityService.deletePerson(isId);
            result =oftenCheckf.isUserAccount(1,"删除成功!");
        }else{
            result = oftenCheckf.isUserAccount(null,"删除失败,当前商品不存在!");
        }
        Tres(result);
    }


    /**根据状态码：sta 查询数据**/
    @RequestMapping(value = "/getEntitySta", method = {RequestMethod.POST, RequestMethod.GET})
    @ResponseBody
    @CrossOrigin
    public void getEntitySta(String sta,String st)
    {
        Integer ista = Integer.parseInt(sta);
        Integer sts = Integer.parseInt(st);
        List<CommodityEntityPo> listCommodity= commodityService.findBystat(ista,sts);
        if(listCommodity != null && listCommodity.size()>0){
            result = oftenCheckf.isUserAccount(listCommodity,"查询成功!");
        }else{
            result = oftenCheckf.isUserAccount(null,"查询失败!");
        }
        Tres(result);
    }


    /**商品上架**/
    @RequestMapping(value = "/UpCommodityEntity", method = {RequestMethod.POST, RequestMethod.GET})
    @ResponseBody
    @CrossOrigin
    public void UpCommodityEntity(String id)
    {
        Integer ids = Integer.parseInt(id);
        int com = commodityService.UpCommodityEntity(ids);
        if(com>0){
            result = oftenCheckf.isUserAccount(1,"上架成功!");
        }else{
            result = oftenCheckf.isUserAccount(null,"上架失败!");
        }
        Tres(result);
    }



    /**商品下架**/
    @RequestMapping(value = "/UpCommodityEntitysta", method = {RequestMethod.POST, RequestMethod.GET})
    @ResponseBody
    @CrossOrigin
    public void UpCommodityEntitysta(String id)
    {
        Integer ids = Integer.parseInt(id);
        int com = commodityService.UpCommodityEntitysta(ids);
        if(com>0){
            result = oftenCheckf.isUserAccount(1,"下架成功!");
        }else{
            result = oftenCheckf.isUserAccount(null,"下架失败!");
        }
        Tres(result);
    }

    //查询指定字段
    @RequestMapping(value = "/getEntityS", method = {RequestMethod.POST, RequestMethod.GET})
    @ResponseBody
    @CrossOrigin
    public void getEntityS(String sta,String st)
    {
        Integer ista = Integer.parseInt(sta);
        Integer sts = Integer.parseInt(st);
        ArrayList<HashMap<String,Object>> listCommodity= oftenCheckf.findCommodityEntityPoSp(ista,sts);
        if(listCommodity != null && listCommodity.size()>0){
            result = oftenCheckf.isUserAccount(listCommodity,"查询成功!");
        }else{
            result = oftenCheckf.isUserAccount(null,"查询失败!");
        }
        Tres(result);
    }



    //根据id查询数据
    @RequestMapping(value = "/getbyId", method = {RequestMethod.POST, RequestMethod.GET})
    @ResponseBody
    @CrossOrigin
    public void getbyId(String id,String email)
    {
        Integer isId = Integer.parseInt(id);
        Optional<CommodityEntityPo>  Optionalommodity = commodityService.findById(isId);
        if(Optionalommodity!=null)
        {  java.util.Date date = DateUtil.date();
            ClickRecord user =new ClickRecord();
            user.setUseremail(email);
            user.setTime(date);
            user.setDityId(id);
            comprehensiveService.InClickRecord(user);
            result = oftenCheckf.isUserAccount(Optionalommodity,"查询成功!");
        }else{

            result = oftenCheckf.isUserAccount(null,"查询失败!");
        }
        Tres(result);
    }


    //加入购物车
    @RequestMapping(value = "/addShoping", method = {RequestMethod.POST, RequestMethod.GET})
    @ResponseBody
    @CrossOrigin
    public void addShoping(HttpServletRequest request)
    {
        String str = request.getParameter("dat");
        JSONObject jsonObject = JSONObject.fromObject(JSON.parseObject(str));
        Tool result =  middleCommoditCheck.addShoppingCard(jsonObject);
        Tres(result);
    }

    //根据email查询购物车
    @RequestMapping(value = "/findShoppimgCard", method = {RequestMethod.POST, RequestMethod.GET})
    @ResponseBody
    @CrossOrigin
    public void findShoppimgCard(String email){

        Tool result = oftenCheckf.findshoppingCard(email);
        Tres(result);

    }


    //根据id删除购物车商品
    @RequestMapping(value = "/DataleShoppimgCard", method = {RequestMethod.POST, RequestMethod.GET})
    @ResponseBody
    @CrossOrigin
    public void DataleShoppimgCard(String id){

        Integer s_id = Integer.parseInt(id);
        try {
            commodityService.DataShoppingCartPo(s_id);
            result = oftenCheckf.isUserAccount(1,"删除成功!");
        } catch (Exception e) {
            result = oftenCheckf.isUserAccount(null,"删除失败!");
            e.printStackTrace();
        }
        Tres(result);
    }

    /**查询当前用户购物车的数量**/
    @RequestMapping(value = "/findCount", method = {RequestMethod.POST, RequestMethod.GET})
    @ResponseBody
    @CrossOrigin
    public void findCount(String email){
        result  = oftenCheckf.findCount(email);
        Tres(result);
    }

    /**创建订单**/
    @RequestMapping(value = "/userOrder", method = {RequestMethod.POST, RequestMethod.GET})
    @ResponseBody
    @CrossOrigin
    public void userOrder(HttpServletRequest request){

        String str = request.getParameter("dat");
        JSONObject jsonObject = JSONObject.fromObject(JSON.parseObject(str));
        Tool result =middleCommoditCheck.AddUserOrder(jsonObject);
        Tres(result);

    }


    @RequestMapping(value = "/findEmail", method = {RequestMethod.POST, RequestMethod.GET})
    @ResponseBody
    @CrossOrigin
    public void findEmail(String email,Integer page,Integer num){

        result  = oftenCheckf.findOrder(email,page,num);
        Tres(result);
    }


    /**删除的当前订单**/
    @RequestMapping(value = "/DeteleOrder", method = {RequestMethod.POST, RequestMethod.GET})
    @ResponseBody
    @CrossOrigin
    public void DeteleOrder(Integer id){

        Tool result =middleCommoditCheck.dateleOrder(id);
        Tres(result);

    }

    /**查询所有订单**/
    @RequestMapping(value = "/findOrder", method = {RequestMethod.POST, RequestMethod.GET})
    @ResponseBody
    @CrossOrigin
    public void findOrder(){
        Tool result =middleCommoditCheck.findOrder();
        Tres(result);
    }


    /**修改发货状态**/
    @RequestMapping(value = "/uporderDate", method = {RequestMethod.POST, RequestMethod.GET})
    @ResponseBody
    @CrossOrigin
    public void uporderDate(Integer id){

        Tool result =middleCommoditCheck.uporderDate(id);
        Tres(result);

    }


    /**修改支付状态**/
    @RequestMapping(value = "/uporderPay", method = {RequestMethod.POST, RequestMethod.GET})
    @ResponseBody
    @CrossOrigin
    public void uporderPay(Integer id){

        Tool result =middleCommoditCheck.uporderPay(id);
        Tres(result);

    }

    /**用户确认收货**/
    @RequestMapping(value = "/confirmReceipt", method = {RequestMethod.POST, RequestMethod.GET})
    @ResponseBody
    @CrossOrigin
    public void confirmReceipt(Integer id){

        Tool result =middleCommoditCheck.confirmReceipt(id);
        Tres(result);

    }


    /**查询相关数据**/
    @RequestMapping(value = "/findOrderList", method = {RequestMethod.POST, RequestMethod.GET})
    @ResponseBody
    @CrossOrigin
    public void findOrderList(Integer paymentStatus, Integer orderStatus,Integer deliverGoods){

        System.out.println(paymentStatus+" "+orderStatus+" "+deliverGoods);
        Tool result =middleCommoditCheck.findOrder(paymentStatus,orderStatus,deliverGoods);
        Tres(result);

    }

    /**查询相关数据**/
    @RequestMapping(value = "/findOrderDetailsAndEmail", method = {RequestMethod.POST, RequestMethod.GET})
    @ResponseBody
    @CrossOrigin
    public void findOrderDetailsAndEmail(Integer paymentStatus, Integer orderStatus,Integer deliverGoods,String email){

        Tool result =middleCommoditCheck.findOrderDetailsAndEmail(paymentStatus,orderStatus,deliverGoods,email);
        Tres(result);

    }


    /**查询相关数据**/
    @RequestMapping(value = "/findCommodityEntityResource", method = {RequestMethod.POST, RequestMethod.GET})
    @ResponseBody
    @CrossOrigin
    public void findCommodityEntityResource(String dsc){
        Tool result =middleCommoditCheck.findCommodityEntityResource(dsc);
        Tres(result);
    }

    /**查询分类商品**/
    @RequestMapping(value = "/findCommodityEntityType", method = {RequestMethod.POST, RequestMethod.GET})
    @ResponseBody
    @CrossOrigin
    public void findCommodityEntityType(String type,String region){

        List<CommodityEntityPo> com = commodityService.findCommodityEntityType(type,region);
        if(com.size()<=0){
            result = oftenCheckf.isUserAccount(null,"查询失败,没有找到相关商品!");
        }else{
            result = oftenCheckf.isUserAccount(com,"查询成功!");
        }

        Tres(result);
    }


    /**商品浏览量记载**/
    @RequestMapping(value = "/findComCount", method = {RequestMethod.POST, RequestMethod.GET})
    @ResponseBody
    @CrossOrigin
    public void findComCount(Integer id){
       Tool result = MiddlelayerCheck.findComCount(id);
        Tres(result);
    }


    /**商品推荐查询**/
    @RequestMapping(value = "/findComserCount", method = {RequestMethod.POST, RequestMethod.GET})
    @ResponseBody
    @CrossOrigin
    public void findComCount(){
        Tool result = MiddlelayerCheck.findComserCount();
        Tres(result);
    }


    /**用户评价**/
    @RequestMapping(value = "/Addevaluated", method = {RequestMethod.POST, RequestMethod.GET})
    @ResponseBody
    @CrossOrigin
    public void Addevaluated(String dityId,String orderuser,String ordrNumber,String textarea){
        Date date = DateUtil.date();
        evaluated eva = new evaluated();
        eva.setDityId(dityId);
        eva.setTextarea(textarea);
        eva.setOrdrNumber(ordrNumber);
        eva.setEvaluatedtime(date);
        eva.setUseremail(orderuser);
        Tool result = middleCommoditCheck.Addevaluated(eva);
        Tres(result);
    }



    /**用户评价**/
    @RequestMapping(value = "/findevaluated", method = {RequestMethod.POST, RequestMethod.GET})
    @ResponseBody
    @CrossOrigin
    public void findevaluated(String dityId){

        Tool result = oftenCheckf.findevaluated(dityId);
        Tres(result);
    }


    /**商品搜索**/
    @RequestMapping(value = "/finSearchProducts", method = {RequestMethod.POST, RequestMethod.GET})
    @ResponseBody
    @CrossOrigin
    public void finSearchProducts(String name,String email){

        Tool result = oftenCheckf.finSearchProducts(name,email);
        Tres(result);
    }






















    public void Tres(Tool result)
    {
        try {
            JsonUtil.toJson(result, response);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }



}
